// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

import VideoGLPlayer, {EventType as GLVideoEventType} from "./VideoGLPlayer";



const { ccclass, property } = cc._decorator;

@ccclass
export default class UIVideoView extends cc.Component {

    @property(cc.Button)
    btnPlay: cc.Button = null;

    @property(cc.Button)
    btnSkip: cc.Button = null;

    @property({type:VideoGLPlayer})
    glPlayer:VideoGLPlayer = null;

    @property({type:cc.VideoPlayer})
    ccPlayer:cc.VideoPlayer = null;


    onLoad() {
        if(this.glPlayer) {
            let hVideoEvent = new cc.Component.EventHandler();
            hVideoEvent.target = this.node; //这个 node 节点是你的事件处理代码组件所属的节点
            hVideoEvent.component = "UIVideoView";//这个是代码文件名
            hVideoEvent.handler = "onGLVideoEvent";

            this.glPlayer.usedCanvas = true;
            this.glPlayer.videoPlayerEvent.push(hVideoEvent);
            this.glPlayer.node.on(cc.Node.EventType.TOUCH_END, this.onClickGLVideo, this);
        }

        if(this.ccPlayer) {
            let hVideoEvent = new cc.Component.EventHandler();
            hVideoEvent.target = this.node; //这个 node 节点是你的事件处理代码组件所属的节点
            hVideoEvent.component = "UIVideoView";//这个是代码文件名
            hVideoEvent.handler = "onCCVideoEvent";

            this.ccPlayer.videoPlayerEvent.push(hVideoEvent);

            this.ccPlayer.node.on(cc.Node.EventType.TOUCH_END, this.onClickCCVideo, this);
        }
    }

    onClickGLVideo(){
        this.glPlayer.play();
    }

    onClickCCVideo() {
        this.ccPlayer.play();
    }

    onDestroy() {
        if(this.glPlayer) {
            this.glPlayer.videoPlayerEvent.length = 0;
            this.glPlayer.node.off("click", this.onClickGLVideo, this);
        }
    }

    onBtnPlay() {
        if(this.ccPlayer)
            this.ccPlayer.node.active = true;
    }

    onBtnSkip() {
        console.log("onBtnSkip")
        this.glPlayer && this.glPlayer.stop();
        this.ccPlayer && this.ccPlayer.stop();
    }

    private onCCVideoEvent(sender: cc.Node, type: cc.VideoPlayer.EventType) {
        switch (type) {
            case cc.VideoPlayer.EventType.PLAYING:
                console.log("cc Playing");
                break;
            case cc.VideoPlayer.EventType.PAUSED:
                console.log("cc Paused");
                break;
            case cc.VideoPlayer.EventType.STOPPED:
                console.log("cc Stopped");
                break;
            case cc.VideoPlayer.EventType.COMPLETED:
                console.log("cc Completed");
                break;
            case cc.VideoPlayer.EventType.META_LOADED:
                console.log("cc MetaLoaded");
                break;
            case cc.VideoPlayer.EventType.READY_TO_PLAY:
                console.log("cc ReadyToPlay");
                this.ccPlayer.play();
                break;
        }
    }

    private onGLVideoEvent(sender: cc.Node, type: GLVideoEventType) {
        switch (type) {
            case GLVideoEventType.Playing:
                console.log("gl Playing");
                break;
            case GLVideoEventType.Paused:
                console.log("gl Paused");
                break;
            case GLVideoEventType.Stopped:
                console.log("gl Stopped");
                break;
            case GLVideoEventType.Completed:
                console.log("gl Completed");
                break;
            case GLVideoEventType.MetaLoaded:
                console.log("gl MetaLoaded");
                break;
            case GLVideoEventType.ReadyToPlay:
                console.log("gl ReadyToPlay");
                break;
        }
    }
}
